﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for cls_tbNHANVIEN
/// </summary>
public class cls_tbNHANVIEN
{
	public cls_tbNHANVIEN()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    #region Function

    #region Insert

    public static string Insert(int F_MANV, string F_HOTENNV, string F_CHUCVU, string @F_USERNAME, string F_PASSWORD, int @F_STTQUYEN, string F_MACN)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        object[] myValue = { F_MANV, F_HOTENNV, @F_CHUCVU, F_USERNAME, F_PASSWORD, F_STTQUYEN, F_MACN };
        string[] myPara = { "@F_MANV", "@F_HOTENNV", "@F_CHUCVU", "@F_USERNAME", "@F_PASSWORD", "@F_STTQUYEN", "@F_MACN" };
        SqlParameter[] Parameter = new SqlParameter[myPara.Length];
        for (int i = 0; i < myPara.Length; i++)
        {
            Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
        }
        try
        {
            if (clsSQL.ExecuteNonQuery("tbNHANVIEN_INSERT", CommandType.StoredProcedure, Parameter) > 0)
                return "OK";
            else
                return "NO";
        }
        catch (SqlException ex) { return ex.Message; }
    }

    #endregion

    #region Delete

    public static string Delete(int F_MANV)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        SqlParameter[] parameter = new SqlParameter[1];

        parameter[0] = new SqlParameter("@F_MANV", F_MANV);
        try
        {
            if (clsSQL.ExecuteNonQuery("tbNHANVIEN_Delete", CommandType.StoredProcedure, parameter) > 0)
                return "OK";
            else return "NO";            
        }
        catch (Exception ex) { return ex.Message; }
    }

    #endregion

    #region Update

    public static string Update(int F_MANV, string F_HOTENNV, string F_CHUCVU, string @F_USERNAME, string F_PASSWORD, int @F_STTQUYEN, string F_MACN, int F_STATUSLOCK)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        object[] myValue = { F_MANV, F_HOTENNV, @F_CHUCVU, F_USERNAME, F_PASSWORD, F_STTQUYEN, F_MACN, F_STATUSLOCK };
        string[] myPara = { "@F_MANV", "@F_HOTENNV", "@F_CHUCVU", "@F_USERNAME", "@F_PASSWORD", "@F_STTQUYEN", "@F_MACN", "@F_STATUSLOCK" };
        SqlParameter[] Parameter = new SqlParameter[myPara.Length];
        for (int i = 0; i < myPara.Length; i++)
        {
            Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
        }
        try
        {
            if (clsSQL.ExecuteNonQuery("tbNHANVIEN_UPDATE", CommandType.StoredProcedure, Parameter) > 0)
                return "OK";
            else
                return "NO";
        }
        catch (SqlException ex) { return ex.Message; }
    }

    public static string resetpass(int F_MANV, string F_PASSWORD)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        object[] myValue = { F_MANV, F_PASSWORD };
        string[] myPara = { "@F_MANV", "@F_PASSWORD" };
        SqlParameter[] Parameter = new SqlParameter[myPara.Length];
        for (int i = 0; i < myPara.Length; i++)
        {
            Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
        }
        try
        {
            if (clsSQL.ExecuteNonQuery("tbNHANVIEN_UPDATE_resetpass", CommandType.StoredProcedure, Parameter) > 0)
                return "OK";
            else
                return "NO";
        }
        catch (SqlException ex) { return ex.Message; }
    }
    /// <summary>
    /// Chỉ gọi khi chắc chắn username là duy nhất
    /// </summary>
    /// <param name="F_USERNAME"></param>
    /// <param name="F_PASSWORD"></param>
    /// <returns></returns>
    public static string Update_Password(string @F_USERNAME, string F_PASSWORD)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        object[] myValue = { F_USERNAME, F_PASSWORD };
        string[] myPara = { "@F_USERNAME", "@F_PASSWORD" };
        SqlParameter[] Parameter = new SqlParameter[myPara.Length];
        for (int i = 0; i < myPara.Length; i++)
        {
            Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
        }
        try
        {
            if (clsSQL.ExecuteNonQuery("tbNHANVIEN_UPDATE_PASSWORD", CommandType.StoredProcedure, Parameter) > 0)
                return "OK";
            else
                return "NO";
        }
        catch (SqlException ex) { return ex.Message; }
    }
    public static string Update_Datetime_Last_Login(string @F_USERNAME)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        object[] myValue = { F_USERNAME };
        string[] myPara = { "@F_USERNAME" };
        SqlParameter[] Parameter = new SqlParameter[myPara.Length];
        for (int i = 0; i < myPara.Length; i++)
        {
            Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
        }
        try
        {
            if (clsSQL.ExecuteNonQuery("tbNHANVIEN_UPDATE_DatetimeLastLogin", CommandType.StoredProcedure, Parameter) > 0)
                return "OK";
            else
                return "NO";
        }
        catch (SqlException ex) { return ex.Message; }
    }
    public static string Update_CheckLastLogin_forSetStatusLock(DateTime datetimenow)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        object[] myValue = { datetimenow };
        string[] myPara = { "@datetimenow" };
        SqlParameter[] Parameter = new SqlParameter[myPara.Length];
        for (int i = 0; i < myPara.Length; i++)
        {
            Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
        }
        try
        {
            if (clsSQL.ExecuteNonQuery("tbNHANVIEN_UPDATE_CheckLastLogin_forSetStatusLock", CommandType.StoredProcedure, Parameter) > 0)
                return "OK";
            else
                return "NO";
        }
        catch (SqlException ex) { return ex.Message; }
    }

    //public static string Update(int id_admin, string username, string password, string notes)
    //{
    //    av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
    //    object[] myValue = { id_admin, username, password, notes };
    //    string[] myPara = { "@Id_Admin", "@Name", "@Password", "@Notes" };
    //    SqlParameter[] Parameter = new SqlParameter[myPara.Length];
    //    for (int i = 0; i < myPara.Length; i++)
    //    {
    //        Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
    //    }
    //    try
    //    {
    //        if (clsSQL.ExecuteNonQuery("ADMIN_Update", CommandType.StoredProcedure, Parameter) > 0)
    //            return "OK";
    //        else return "NO";
    //    }
    //    catch (Exception ex) { return ex.Message; }
    //}

    //public static string UpdateUser(int id_admin, string username, string notes, int Limit)
    //{
    //    av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
    //    object[] myValue = { id_admin, username, notes, Limit };
    //    string[] myPara = { "@Id_Admin", "@Name", "@Notes", "@Limit" };
    //    SqlParameter[] Parameter = new SqlParameter[myPara.Length];
    //    for (int i = 0; i < myPara.Length; i++)
    //    {
    //        Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
    //    }
    //    try
    //    {
    //        if (clsSQL.ExecuteNonQuery("ADMIN_UpdateUser", CommandType.StoredProcedure, Parameter) > 0)
    //            return "OK";
    //        else return "NO";
    //    }
    //    catch (Exception ex) { return ex.Message; }
    //}
    #endregion

    #region CheckAdmin

    /// <summary>
    /// Ham kiem tra tai khoan admin
    /// </summary>
    /// <param name="username">Ten tai khoan dang nhap</param>
    /// <param name="password">Mat khau dang nhap</param>
    /// <returns></returns>
    public static bool CheckAccount(string F_USERNAME, string F_PASSWORD)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        object[] myValue = { F_USERNAME, F_PASSWORD };
        string[] myPara = { "@F_USERNAME", "@F_PASSWORD" };
        SqlParameter[] Parameter = new SqlParameter[myPara.Length];
        DataTable table = new DataTable();
        for (int i = 0; i < myPara.Length; i++)
        {
            Parameter[i] = new SqlParameter(myPara[i], myValue[i]);
        }
        try
        {
            table = clsSQL.GetDataFrom("tbNHANVIEN_checkAccount", CommandType.StoredProcedure, Parameter);
            if (table.Rows.Count == 1)
                return true;
            else return false;
        }
        catch /*(Exception ex)*/ { return false; }
    }

    //public static bool CheckUser(string F_USERNAME)
    //{
    //    av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
    //    DataTable table = new DataTable();
    //    SqlParameter[] Parameter = new SqlParameter[1];
    //    Parameter[0] = new SqlParameter("@F_USERNAME", F_USERNAME);
    //    try
    //    {
    //        table = clsSQL.GetDataFrom("tbNHANVIEN_CheckUser", CommandType.StoredProcedure, Parameter);
    //        if (table.Rows.Count > 0)
    //            return true;
    //        else return false;
    //    }
    //    catch (Exception ex) { return false; }
    //}
    public static DataTable GetByF_MANV(int F_MANV)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        DataTable table = new DataTable();
        SqlParameter[] Parameter = new SqlParameter[1];
        Parameter[0] = new SqlParameter("@F_MANV", F_MANV);
        try
        {
            table = clsSQL.GetDataFrom("tbNHANVIEN_GetByF_MANV", CommandType.StoredProcedure, Parameter);
        }
        catch /*(Exception ex)*/ { }
        return table;
    }
    public static string GetF_HOTENNVByF_MANV(int F_MANV)
    {
        string rs = "";
        DataTable dt = new DataTable();
        dt = GetByF_MANV(F_MANV);
        if (dt.Rows.Count > 0)
        {
            rs = dt.Rows[0]["F_HOTENNV"].ToString();
        }
        return rs;
    }    
    public static DataTable GetByUser(string F_USERNAME)
    {
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        DataTable table = new DataTable();
        SqlParameter[] Parameter = new SqlParameter[1];
        Parameter[0] = new SqlParameter("@F_USERNAME", F_USERNAME);
        try
        {
            table = clsSQL.GetDataFrom("tbNHANVIEN_GetByUser", CommandType.StoredProcedure, Parameter);
        }
        catch /*(Exception ex)*/ { }
        return table;
    }
    public static int GetMaxID()
    {
        int result = 0;
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        DataTable table = new DataTable();
        //SqlParameter[] Parameter = new SqlParameter[1];
        //Parameter[0] = new SqlParameter("@F_USERNAME", F_USERNAME);
        
        table = clsSQL.GetDataFrom("tbNHANVIEN_GetMaxID", CommandType.StoredProcedure, null);
        try
        {
            result = int.Parse(table.Rows[0][0].ToString());
        }
        catch { result = 0; }
        return result;
    }
    public static bool CheckNewUserValid(string F_USERNAME)
    {
        bool result = false;
        DataTable table = new DataTable();
        table = GetByUser(F_USERNAME);
        if (table.Rows.Count > 0)
        {
            result = false;
        }
        else
        {
            result = true;
        }
        return result;
    }
    /// <summary>
    /// Ham tim admin
    /// </summary>
    /// <param name="username">Ten admin can tim</param>
    /// <returns></returns>
    //public static DataTable SearchAdmin(string username, string pass)
    //{
    //    av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
    //    DataTable table = new DataTable();
    //    SqlParameter[] parameter = new SqlParameter[2];
    //    parameter[0] = new SqlParameter("@Name", username);
    //    parameter[1] = new SqlParameter("@Password", pass);
    //    try
    //    {
    //        table = clsSQL.GetDataFrom("ADMIN_SearchAdmin", CommandType.StoredProcedure, parameter);
    //    }
    //    catch (Exception ex) { }
    //    return table;
    //}
    #endregion

    #region Get + Set

    //public static string SetCurrent(string username)
    //{
    //    av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
    //    DataTable table = new DataTable();
    //    SqlParameter[] Parameter = new SqlParameter[1];
    //    Parameter[0] = new SqlParameter("@Name", username);
    //    try
    //    {
    //        if (clsSQL.ExecuteNonQuery("ADMIN_SetCurrent", CommandType.StoredProcedure, Parameter) > 0)
    //            return "OK";
    //        else return "NO";
    //    }
    //    catch (Exception ex) { return ex.ToString(); }
    //}
    //public static DataTable GetCurrent()
    //{
    //    av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
    //    DataTable table = new DataTable();
    //    try
    //    {
    //        table = clsSQL.GetDataFrom("ADMIN_GetCurrent", CommandType.StoredProcedure, null);
    //    }
    //    catch (Exception ex) { }
    //    return table;
    //}
    //public static DataTable GetAdmin(int adminID)
    //{
    //    DataTable table = new DataTable();
    //    av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
    //    SqlParameter[] parameter = new SqlParameter[1];
    //    parameter[0] = new SqlParameter("@Id_Admin", adminID);
    //    try
    //    {
    //        table = clsSQL.GetDataFrom("ADMIN_GetAdmin", CommandType.StoredProcedure, parameter);
    //    }
    //    catch (Exception ex) { }
    //    return table;
    //}        //Id_admin = 0 lay tat ca
    public static DataTable Get_manv_hoten_NHANVIEN()
    {
        DataTable table = new DataTable();
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        //SqlParameter[] parameter = new SqlParameter[1];
        //parameter[0] = new SqlParameter("@Id_Admin", adminID);
        try
        {
            //table = clsSQL.GetDataFrom("ADMIN_GetAllAdmin", CommandType.StoredProcedure, null);
            table = clsSQL.GetDataFrom("select F_MANV,F_HOTENNV from tbNHANVIEN", CommandType.Text, null);
            
        }
        catch /*(Exception ex)*/ { }
        return table;
    }
    public static DataTable GetAll_NHANVIEN()
    {
        DataTable table = new DataTable();
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        //SqlParameter[] parameter = new SqlParameter[1];
        //parameter[0] = new SqlParameter("@Id_Admin", adminID);
        try
        {
            //table = clsSQL.GetDataFrom("ADMIN_GetAllAdmin", CommandType.StoredProcedure, null);
            table = clsSQL.GetDataFrom("tbNHANVIEN_GetAll", CommandType.StoredProcedure, null);

        }
        catch /*(Exception ex)*/ { }
        return table;
    }
    public static DataTable GetAll_NHANVIEN_QUYEN()
    {
        DataTable table = new DataTable();
        av.dal.SqlDataProvider clsSQL = new av.dal.SqlDataProvider();
        //SqlParameter[] parameter = new SqlParameter[1];
        //parameter[0] = new SqlParameter("@Id_Admin", adminID);
        try
        {
            //table = clsSQL.GetDataFrom("ADMIN_GetAllAdmin", CommandType.StoredProcedure, null);
            table = clsSQL.GetDataFrom("tbNHANVIEN_GetAll_NHANVIEN_QUYEN", CommandType.StoredProcedure, null);

        }
        catch /*(Exception ex)*/ { }
        return table;
    }
    #endregion

    #region get by chi nhánh
    public static DataTable GetByBranch(string F_MACN)
    {
        DataTable dt = new DataTable();
        DatabaseConnect connect = new DatabaseConnect();
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@F_MACN", F_MACN);
        try
        {
            dt = connect.GetSelectCommand("tbNHANVIEN_GetByChiNhanh", para);
        }
        catch
        { 
           
        }
        return dt;
    }
    #endregion

    public static DataTable GetNV_ByChinhanh(string F_MACN)
    {
        DataTable dt = new DataTable();
        DatabaseConnect connect = new DatabaseConnect();
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@F_MACN", F_MACN);
        try
        {
            dt = connect.GetSelectCommand("tbNHANVIEN_GetNV_ByChiNhanh", para);
        }
        catch
        {

        }
        return dt;
    }
    #endregion
}